using MediatR;
using B.S.XZYData.ErrorCode;
using B.S.XZYData.Api.Read.Application.Command.Team;
using B.S.XZYData.Infrastructure;
using B.S.XZYData.Domain.XZY;
using B.S.XZYData.Domain.RBAC;
using SqlSugar;

namespace B.S.XZYData.Api.Read.Application.Handler.Team
{
    /// <summary>
    /// 获取班组分页数据处理器
    /// </summary>
    public class GetTeamPageHandler : IRequestHandler<GetTeamPageCommand, ApiPageing<TeamDTO>>
    {
        private readonly MyDbContext _dbContext;

        public GetTeamPageHandler(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task<ApiPageing<TeamDTO>> Handle(GetTeamPageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 查询班组数据
                var teams = _dbContext.Queryable<B.S.XZYData.Domain.XZY.Team>()
                    .Where(x => !x.IsDeleted)
                    .ToList();

                // 查询用户数据用于获取创建人和修改人姓名
                var accounts = _dbContext.Queryable<AccountModel>()
                    .Where(x => !x.IsDeleted)
                    .ToList();

                // 应用筛选条件
                if (!string.IsNullOrEmpty(request.TeamName))
                {
                    teams = teams.Where(x => x.TeamName.Contains(request.TeamName)).ToList();
                }

                if (!string.IsNullOrEmpty(request.TeamCode))
                {
                    teams = teams.Where(x => x.TeamCode.Contains(request.TeamCode)).ToList();
                }

                if (request.TeamType.HasValue)
                {
                    teams = teams.Where(x => (int)x.TeamType == request.TeamType.Value).ToList();
                }

                // 计算总数
                var total = teams.Count;

                // 排序和分页
                var pagedTeams = teams
                    .OrderByDescending(x => x.CreateTime)
                    .Skip((request.Index - 1) * request.Size)
                    .Take(request.Size)
                    .ToList();

                // 转换为DTO
                var teamDTOs = pagedTeams.Select(team => new TeamDTO
                {
                    Id = team.Id,
                    TeamName = team.TeamName,
                    TeamCode = team.TeamCode,
                    TeamType = team.TeamType,
                    Remark = team.Remark,
                    Creator = accounts.FirstOrDefault(a => a.Id == team.CreatorId)?.RealName ?? "",
                    CreateTime = team.CreateTime,
                    Modifier = team.ModifierId.HasValue ? accounts.FirstOrDefault(a => a.Id == team.ModifierId)?.RealName ?? "" : "",
                    ModifyTime = team.ModifyTime
                }).ToList();

                return new ApiPageing<TeamDTO>
                {
                    PageData = teamDTOs,
                    TotaCount = total,
                    Code = ApiEnum.Success
                };
            }
            catch (Exception)
            {
                return new ApiPageing<TeamDTO>
                {
                    PageData = new List<TeamDTO>(),
                    TotaCount = 0,
                    Code = ApiEnum.Error
                };
            }
        }


    }
}
 